package com.dao;

import com.entity.Goods;
import java.util.List;
import java.util.Optional;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Mapper;

/**
 * Goods 实体的 MyBatis 映射接口。
 */
@Mapper
public interface GoodsDao {

    /**
     * 根据 ID 查询商品
     *
     * @param id 商品 ID
     * @return 包含查询结果的 Optional 对象，若未找到则为空 Optional
     */
    @Select("SELECT id, name, description, user_id AS userId, " +
            "created_at AS createTime, updated_at AS updateTime " +
            "FROM products WHERE id = #{id}")
    Optional<Goods> findById(Long id);

    /**
     * 保存商品
     *
     * @param goods 要保存的商品对象
     * @return 受影响的行数
     */
    @Insert("INSERT INTO products (name, description, user_id, created_at, updated_at) " +
            "VALUES (#{name}, #{description}, #{userId}, NOW(), NOW())")
    @Options(useGeneratedKeys=true, keyProperty="id")
    int save(Goods goods);

    /**
     * 查询所有商品列表
     *
     * @return 商品列表
     */
    @Select("SELECT id, name, description, user_id AS userId, " +
            "created_at AS createTime, updated_at AS updateTime " +
            "FROM products")
    List<Goods> findAll();

} 